package com.yuanwushe.blue.materialapp;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private SwipeRefreshLayout mSwipeRefresh;
    private RecyclerView mRecyclerView;
    private List<String> mData = new ArrayList<>();
    private TextAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_24dp);
        mDrawerLayout = findViewById(R.id.drawer);
        mNavigationView = findViewById(R.id.nav_view);
        mNavigationView.setCheckedItem(R.id.nav_call);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
        mSwipeRefresh = findViewById(R.id.swipe_refresh);
        mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        }
                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "刷新了", Toast.LENGTH_SHORT).show();
                                refreshListener(false);
                                mSwipeRefresh.setRefreshing(false);
                            }
                        });
                    }
                }).start();
            }
        });
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new TextAdapter(mData);
        mRecyclerView.setAdapter(mAdapter);
        refreshListener(true);
    }

    private void refreshListener(boolean isFirst){
        mData.clear();
        if (!isFirst) {
            mData.add("刷新");
        }
        mData.add("水果");
        mData.add("苹果");
        mData.add("啦啦");
        mData.add("问问");
        mData.add("嗯嗯");
        mData.add("让人");
        mData.add("请求");
        mData.add("天天");
        mData.add("上上");
        mData.add("下下");
        mData.add("站长");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.delete:
                Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.download:
                Toast.makeText(this, "download", Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(this, "setting", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            default:
        }
        return true;
    }

    class TextAdapter extends RecyclerView.Adapter<TextAdapter.TextHolder>{

        public List<String> mData;

        public TextAdapter(List<String> data){
            mData = data;
        }

        @Override
        public TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LinearLayout.inflate(parent.getContext(), R.layout.text_item, null);
            return new TextHolder(view);
        }

        @Override
        public void onBindViewHolder(TextHolder holder, final int position) {
            holder.textView.setText(mData.get(position));
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this, FruitActivity.class);
                    intent.putExtra("name", mData.get(position));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public class TextHolder extends RecyclerView.ViewHolder{
            public TextView textView;
            public View view;
            public TextHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.text);
                view = itemView;
            }
        }
    }
}
